import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class Login {
    Scanner sc = new Scanner(System.in);
    public void login(){
        System.out.println("您好，欢迎登陆学生考试系统Welcome to exam system，请输入您的用户名和密码(用户名username:密码key)：");
        String input = sc.next();
        if(superManagerLogin(input)){
            HomeInterface.homeContent();
        } else {
            System.out.println("Wrong username or key!");
        }
}

    public Boolean superManagerLogin(String input) {
        String username = input.split(":")[0];
        String key = input.split(":")[1];
        String sql = "SELECT key1 FROM super_manager WHERE name=?";
        try {
            Connection conn = DatabaseUtil.getConnection();
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, username);
            ResultSet rs = ptmt.executeQuery();
            String realKey = "";
            while (rs.next()) {
                realKey = rs.getString("key1");
            }
            if (key.equals(realKey)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
